package training;

/**
 * @Author liangzai
 * @Description:
 */
public class J25_8_29_Training {
    //Alice 和 Bob 玩鲜花游戏
    public long flowerGame(int n, int m) {
        long upOdd = 0, upEven = 0, downOdd = 0, downEven = 0;
        for (int i = 1; i <= n; i++) {
            if (i % 2 == 0) {
                upEven++;
            }else {
                upOdd++;
            }
        }
        for (int i = 1; i <= m; i++) {
            if (i % 2 == 0) {
                downEven++;
            }else {
                downOdd++;
            }
        }
        return upOdd * downEven + upEven * downOdd;
    }

    //下一个数
    public int[] findClosedNumbers(int num) {
        int up = num + 1;//向上枚举
        int down = num - 1;//向下枚举
        int count = findOneCount(num);//num的1的个数
        while (findOneCount(up) != count) {
            up++;
            if (up < 0) {//越界了那就是找不到，设置为-1
                up = -1;
                break;
            }
        }
        while (findOneCount(down) != count) {
            down--;
            if (down < 0) {//变为负数了那就是找不到了，设置为-1
                down = -1;
                break;
            }
        }
        return new int[]{up, down};
    }
    //求数的二进制1的个数
    private static int findOneCount(int num) {
        int count = 0;
        while (num != 0) {
            num &= num - 1;
            count++;
        }
        return count;
    }
}
